﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using BackupRestore.Utils;
using BackupRestore.Gui;
using BackupRestore.BackupRestoreFramework;

namespace BackupRestore
{

    /// <summary>
    /// BackupRestoreController class
    /// </summary>
    public class BackupRestoreController
    {
        
        private BackupRestoreUIController BackupRestoreUIRef; // Reference to the UI

        private static string _className;

        public BackupRestoreController (RunMode runMode)
        {
            _className = MethodBase.GetCurrentMethod().DeclaringType.Name;

            BackupUtils.Init();

            Logger.Logger.LoggerDebugMode = runMode.RequestedDebugMode;//set the runMode
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
        
            BackupRestoreUIRef = new BackupRestoreUIController(this);//create UI for this
            Application.Run(BackupRestoreUIRef);//run it
        }

        /// <summary>
        /// Start shut down - prepares all for exiting
        /// </summary>
        private void InitiateShutdown()
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);

            Logger.Logger.LogDebug(_className, _methodName, "Shutting down system");

            Logger.Logger.Exiting();

            Environment.Exit(0);

            Logger.Logger.LogMethodExit(_className, _methodName);
        }

        /// <summary>
        /// Called by the UI when the user requests to close the window.
        /// </summary>
        internal void WindowCloseRequested()
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);

            Logger.Logger.LogDebug(_className, _methodName, "System shutdown requested");

            InitiateShutdown();

            Logger.Logger.LogMethodExit(_className, _methodName);
        }

        /// <summary>
        /// Called by the UI when user requests backup
        /// </summary>
        /// <param name="Filename">File to store the backup in</param>
        /// <param name="Specification">The specification to back up</param>
        internal void StartBackup (string Filename, BackupSpecification Specification)
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry (_className, _methodName);

            Logger.Logger.LogDebug(_className, _methodName, "Start Backup requested: [FILE NAME] " + Filename);
            Logger.Logger.LogDebug(_className, _methodName, "Start Backup requested: [SPEC] " + Specification.ToString());

            Logger.Logger.LogMethodExit(_className, _methodName);
        }

        /// <summary>
        /// Called by the UI when backup analysis starts (before the backup)
        /// </summary>
        /// <param name="pathsToBackup">a list of string paths to backup</param>
        internal void StartPrebackupAnalysis (string [] pathsToBackup)
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);

            BackupSpecification Specification = BackupUtils.BuildBackUpSpecification(pathsToBackup);

            Logger.Logger.LogDebug(_className, _methodName, "Pre Backup Analysis requested");

            PreBackupAnalyzer analyzer = new PreBackupAnalyzer(Specification);
            analyzer.Analyze();


            Logger.Logger.LogMethodExit(_className, _methodName);
        }

        /// <summary>
        /// Called by the UI when user requests restore
        /// </summary>
        /// <param name="Filename">File to restore from</param>
        internal void StartRestore(string Filename)
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);

            Logger.Logger.LogDebug(_className, _methodName, "Restore requested: [FILE NAME] " + Filename);

            Logger.Logger.LogMethodExit(_className, _methodName);
        }


    }
}
